from flask import Flask, request
from datetime import datetime
from dingtalkchatbot.chatbot import DingtalkChatbot

app = Flask(__name__)
# 设置 Webhook 地址
url = "https://oapi.dingtalk.com/robot/send?access_token=c54fa6837d584b0abc56c0a7d0456ca37d59213dcc7ea3f6f104b20de3c71284"
secret = "SEC9de9f15e7d9bc3be3c0c5f50adcf283ff26189940919486b9cdb0feda9ee45b1"
# 初始化机器人
bot = DingtalkChatbot(webhook=url, secret=secret)


def nowTime():
    now = datetime.now()
    # 将时间格式化为字符串
    formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")
    return formatted_time


@app.route("/webhook", methods=["POST"])
def webhook():
    # 处理来自 Webhook 的请求数据
    data = request.get_json()

    # 做相关操作
    nowtime = nowTime()
    msg = "*{} ，{} 域名证书准备到期，敬请关注！*".format(nowtime, data["content"])
    # 将 print 的拼接内容作为消息发送给钉钉
    bot.send_text(msg)
    print(msg)

    # 在这里添加您希望执行的逻辑，处理 Webhook 数据
    return msg


if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)
